define(["backbone"], function () {
    var routesMap = {
        "":"report.overview",
        "report/:view":"report.view",

        "media(/index)":"media.index",
        "media/adspace/:id":"media.adspacereport",
        "media/:view":"media.view",

        "adspace(/index)":"adspace.index",
        "adspace/detail(/:id)":"adspace.detail",

        "template(/index)":"template.index",
        "template/detail(/:id)":"template.detail",

        "dsp(/index)":"dsp.index",
        "dsp/detail(/:id)":"dsp.detail",
        "dsp/consume/:id":"dsp.consumereport",
        "dsp/bid/:id":"dsp.bidreport",
        "dsp/:view":"dsp.view",

        "customer(/index)":"customer.index",
        "customer/detail(/:id)":"customer.detail",

        "creative(/index)":"creative.index",
        "creative/detail(/:id)":"creative.detail",

        //":controller(/:view)(/:id)":"controller.view",
        "login":"account.login",
        "*error": "defaultError"
    };
    var Router = Backbone.Router.extend({
        routes: routesMap
    });
    var router = new Router();
    //彻底用on route接管路由的逻辑，这里route是路由对应的value
    router.on("route", function (key, params) {
        reqModalCommon.loadingStart();
        $.ExtNotify.destroy();
        var routeInfo=routerCommon.getRouterInfo(key, params);
        if(routerCommon.checkExistPath(routeInfo.controller,routeInfo.view)){
            switch(key){
                case "defaultError":
                    $.ExtNotify.error("Url不存在！");
                    break;
                case "account.login":
                    if(router.layoutIndexView){
                        routerCommon.stopListening(router.layoutIndexView);
                        router.layoutIndexView=null;
                    }
                    require(["css!../css/pageLogin.css"], function () {
                        require(backboneCommon.getViewByArr(["Layout.Login"]), function (View) {
                            var view = new View();
                            routerCommon.initRouterPage(routeInfo);
                        });
                    });
                    break;
                default:
                    routerCommon.initLayoutIndex(function(){
                        routerCommon.initRouterPage(routeInfo);
                    });
            }
        }
    });
    var routerCommon={
        getRouterInfo:function (key, params) {
            var paramLower=[];
            for(var pi=0;pi<params.length;pi++){
                paramLower.push(params[pi]?params[pi].toLowerCase():params[pi])
            }
            var router=key;
            var controller="";
            var view="";
            var arrParam=paramLower;
            if(key!="defaultError"){
                switch(key){
                    case "controller.view":
                        controller=paramLower[0];
                        view=paramLower[1];
                        router=controller+"."+view;
                        arrParam=[];
                        break;
                    default:
                        var temp=key.split(".");
                        controller=temp[0];
                        view=temp[1];
                        router=key;
                        if(view=="view"){
                            view=paramLower[0];
                            router=controller+"."+view;
                            arrParam=[];
                        }
                }
            }
            return {router:router,controller:controller,view:view,param:arrParam}
        },
        checkExistPath:function (controller,view) {
            var b=false;
            if(controller.length>0 && view.length>0){
                b=backboneCommon.checkExistPath(controller,view);
                if(!b){$.ExtNotify.error("Url不存在")}
            }else{
                $.ExtNotify.error("Url不存在");
            }
            return b
        },
        initLayoutIndex:function(cb){
            var token=$.cookie('infoToken');
            if(token){
                ////todo 检查是否登录
                $.ajax({url:"assets/data/account/check.json",type:"GET",success:function (res) {
                    if(res.ret.toString()=="200"){
                        if(router.layoutIndexView){
                            cb()
                        }else {
                            require(backboneCommon.getViewByArr(["Layout.Index"]), function (View) {
                                var view = new View();
                                router.layoutIndexView = view.view;
                                cb()
                            });
                        }
                    }else{
                        window.location.href="#login";
                    }
                }});
            }else{
                window.location.href="#login";
            }
        },
        initRouterPage:function(json){
            var self=this;
            var pageInfo=backboneCommon._getMvcInfo(json.router);
            backboneCommon.setTitleName(pageInfo.page.title);
            if(pageInfo.page.menuId){router.layoutIndexView.setMenu(pageInfo.page.menuId);}
            require(backboneCommon.getControllerByArr(pageInfo.controller), function (Controller) {
                var controller=new Controller();
                require(backboneCommon.getViewByArr(pageInfo.view),function(View){
                    var view=new View(json.param,json);
                    if (router.currentView && router.currentView !== view.view) {
                        self.stopListening(router.currentView);
                        if(router.currentView.viewAttach){
                            for(var v in router.currentView.viewAttach){
                                if (router.currentView.viewAttach.hasOwnProperty(v)) {
                                    self.stopListening(router.currentView.viewAttach[v]);
                                }
                            }
                        }
                    }
                    router.currentView=view.view;
                    if(pageInfo.page.view.length>0){
                        if(controller[pageInfo.page.view]){
                            controller[pageInfo.page.view](json.param,json);
                        }
                    }
                    reqModalCommon.loadingEnd();
                });
            });
        },
        stopListening:function (view){
            view.undelegateEvents();
            view.stopListening();
        }
    };
    return router;
});